define(['jquery'],()=>{
    class HeadShop{
        constructor(){
            this.loadHtml().then(()=>{
                this.setCart()
                this.span()
                this.user()
                this.tuiChu()
            })
        }
        loadHtml(){
            return new Promise(resolve=>{
                $('header').load('/html/header-shop.html',resolve)
            })
        }

        setCart() {
            let cart = localStorage.getItem('cart')
            let count
            if(cart){
                cart = JSON.parse(cart)
                count = cart.reduce((prev,item)=>{
                    return  prev += item.count
                },0)
            }
            $('#headCart span,#rightCart span').html(count)
        }

        span (){
            let str = $('#headCart span').html()
            if(str){
                $('#headCart span').show()
            }else{
                $('#headCart span').hide()
            }
        }

        user () {
            const user = JSON.parse(localStorage.getItem('user'))
            if(user){
                let name = ''
                  user.map(item=>{
                      name = item.name
                      return item
                  })
                $('.user').hide()
                $('.users').show().children('#name').html(name)
            }
        }

        tuiChu() {
            $('.tuiChu').on('click',function(){
                $('.user').show()
                $('.users').hide().children('#name').html('')
            })
        }
    }
    return new HeadShop()
})